package search.restapi;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import search.config.MediaTypes;
import search.restapi.ManageRestAPI;
import search.service.graph.EntityExpansionService;
import search.service.graph.EntitySearchService;


import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

/**
 * Created by julianzliu on 2016/7/3.
 */

@Api(value="graph", description="Graph Rest Api")
@Path("/v1/graph")
@Produces(MediaTypes.JSON_UTF8)
@Component
public class GraphRestAPI {
    final Logger logger = LoggerFactory.getLogger(GraphRestAPI.class);

    private final ObjectMapper mapper;

    private final EntitySearchService entitySearchService;
    private final EntityExpansionService entityExpansionService;

    @Inject
    public GraphRestAPI(
            ObjectMapper mapper,
            EntitySearchService entitySearchService,
            EntityExpansionService entityExpansionService
    ) {

        this.mapper = mapper;
        this.entitySearchService = entitySearchService;
        this.entityExpansionService = entityExpansionService;
    }


    @PostConstruct
    public void init(){
        logger.info("Initialized /v1/graph/ Successfully!");
    }


    @GET
    @Path("/_entity_search")
    @Produces(MediaTypes.JSON_UTF8)
    public String entitySearchGet(
            @QueryParam("q") String q
    ){
        if(q == null) q = "";
        return entitySearchService.entitySearchService( q );
    }

    @GET
    @Path("/_entity_expansion")
    @Produces(MediaTypes.JSON_UTF8)
    public String entityExpansionGet(
            @QueryParam("q") String q
    ){
        if(q == null) q = "";
        return entityExpansionService.entityExpansionService( q );
    }
}
